import { Component, EventEmitter, Injector, OnInit, Output, ViewChild } from '@angular/core';
import { AppModalComponentBase } from '@shared/common/app-modal-component-base';
import { Content2ServiceProxy, GetSharedInfoInput } from '@shared/service-proxies/service-proxies';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { finalize } from 'rxjs/operators';
import { ISelectInfoItemOption } from './select-info-item-option';


@Component({
    templateUrl: './shared-info-modal.component.html'
})
export class SharedInfoModalComponent  extends AppModalComponentBase{
  title?: string;
  type: boolean;
  data: any[] = [];

  isLoading = false;
  option: ISelectInfoItemOption;

  constructor(
    public bsModalRef: BsModalRef,
    injector: Injector,
    private _contentService: Content2ServiceProxy) {
    super(injector);
  }

  shown(){
    this.isLoading = true;

    const input = new GetSharedInfoInput({
        id: this.option.id,
        menuId: this.option.kind,
        relateId: this.option.relateId
    });

    this._contentService.getSharedInfo(input)
        .pipe(finalize(() => this.isLoading = false))
        .subscribe(result => {
          this.data = result.items;
          this.type = result.type;
          if (this.type) {
            this.title = "分享至以下栏目：";
          } else {
            this.title = "来源于以下栏目：";
          }
        });
  }  
  
  close(): void {
    this.bsModalRef.hide();
  }
}
